package lib.core.com
{
	import lib.core.ApplicationFacade;
	import lib.core.com.modules.BaseModule;
	
	import mx.core.Application;
	import mx.core.Container;
	import mx.events.FlexEvent;
	import mx.events.ModuleEvent;
	
	import urlkit.rules.FlexBrowserManagerAdapter;
	import urlkit.rules.UrlRuleSet;
	import urlkit.rules.UrlValueRule;

	public class BaseApplication extends Application
	{
		protected var _container:Container;
		private var facade:ApplicationFacade = ApplicationFacade.getInstance();
		private var controller:FlexBrowserManagerAdapter = new FlexBrowserManagerAdapter();
		public var route:UrlRuleSet = new UrlRuleSet();
		
		private var firstRule:UrlValueRule = new UrlValueRule();
		private var secondRule:UrlValueRule = new UrlValueRule();
		
		public function get container():Container
		{
			return this._container;
		}

		public function BaseApplication()
		{
			super();
			this._container = this;
			this.addEventListener(FlexEvent.INITIALIZE, onInit);
					
			this.route.type = UrlRuleSet.ONE;
			this.firstRule.urlFormat = '*';
			this.secondRule.urlFormat = '*/*';
			this.route.addChild(this.firstRule);
			this.route.addChild(this.secondRule);
			this.controller.applicationState = this.route;
			
			this.controller.initialized(this, "controller");
		}
		
		private function onInit(e:FlexEvent):void
		{
			this.facade.startup(this);	
			this.init();
		}
		
		protected function init():void
		{
			
		}
		public function onModuleSetup(e:ModuleEvent):void
		{
			
		}
		public function onModuleLoading(e:ModuleEvent):void
		{
			
		}
		public function onModuleLoaded():void
		{
			
		}
		public function deactivateModules(exception:BaseModule = null):void
		{
			for each(var module:BaseModule in this.container.getChildren())
			{
				if (!exception || exception != module)
				{
					module.visible = false;
					module.includeInLayout = false;
					module.resetToDefaults();
				}
			}	
		}
	}
}